package com.architecture.util.string;

import java.text.Collator;
import com.architecture.util.object.ObjectUtility;

/**
 * <p>This class has utilities methods for be used with Strings.</p>
 * @author JR
 * @version 1.0
 * @category String Utilities
 */
public final class StringUtility {
	
	/**
	 * <p>Represents a empty String.</p>
	 */
	public static final String EMPTY_STRING = "";
	
	/**
	 * private constructor of <code>StringUtility</code>.
	 */
	private StringUtility() {
		super();
	}
	
	/**
	 * <p>Verify if a String is empty</p>
	 * @param source
	 * @return <code>Boolean</code>
	 */
	public static Boolean isEmpty(final String source){
		if(ObjectUtility.isRef(source)){
			return source.equals(EMPTY_STRING);	
		}else{
			return Boolean.FALSE;
		}
	}
	
	/**
	 * <p>Verify if a String is not empty</p>
	 * @param source
	 * @return <code>Boolean</code>
	 */
	public static Boolean isNotEmpty(final String source){
		return !isEmpty(source);
	}

	/**
	 * <p>Compare if two strings are equals using the default locale.</p>
	 * @param source
	 * @param target
	 * @return <code>Boolean</code>
	 */
	public static Boolean equals(final String source, final String target){
		if(ObjectUtility.isNotRef(source) && ObjectUtility.isNotRef(target)){
			final Collator collator = Collator.getInstance();
			collator.setStrength(Collator.PRIMARY);
			return collator.equals(source, target);
		}else{
			return Boolean.FALSE;
		}
	}
	
	/**
	 * <p>Repeat a string using times variable to repeat the string passed by parameter.</p>
	 * @param source
	 * @param times
	 * @return <code>String</code>
	*/
	public static String repeat(final String source,final int times){
		if(ObjectUtility.isRef(source) && StringUtility.isNotEmpty(source)){
			if(times > 0){
				final StringBuilder sb = new StringBuilder();
				for(int i=0;i <= times;i++){
					sb.append(source);
				}
				return sb.toString();
			}else{
				throw new IllegalArgumentException();
			}
		}else{
			return source;
		}
	}
	
}
